/*
 *    Copyright (c) 2018-2025, lengleng All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the pig4cloud.com developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: lengleng (wangiegie@gmail.com)
 */
package com.excesys.exsecs.om.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.excesys.exsecs.om.dto.*;
import com.excesys.exsecs.om.entity.*;
import com.excesys.exsecs.om.mapper.*;
import com.excesys.exsecs.om.service.OmInspectionItemsService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @date 2023-05-19
 */
@RequiredArgsConstructor
@Service
public class OmInspectionItemsServiceImpl extends ServiceImpl<OmInspectionItemsMapper, OmInspectionItems>
		implements OmInspectionItemsService {

	private final OmInspectionItemsMapper omInspectionItemsMapper;

	private final OmInspectionAreaClassificationMapper omInspectionAreaClassificationMapper;

	private final OmDeviceModelClassificationMapper omDeviceModelClassificationMapper;

	private final OmEvenAndDeviceMapper omEvenAndDeviceMapper;

	private final OmEvenAndFacilityMapper omEvenAndFacilityMapper;

	private final OmEvenAndRegionMapper omEvenAndRegionMapper;

	@Override
	public List<EventAndRegionRespDto> getInspectionList(String regionFacilityName, String departmentName) {
		List<EventAndRegionRespDto> list = omInspectionItemsMapper.getList(regionFacilityName, departmentName);
		for (EventAndRegionRespDto dto : list) {
			if (StringUtils.isNotEmpty(dto.getItemsId()) && StringUtils.isNotEmpty(dto.getId())) {
				if (dto.getItemsId() != dto.getId()) {
					dto.setId(dto.getItemsId());
					OmInspectionItems oi = omInspectionItemsMapper.selectById(dto.getItemsId());
					dto.setEvenInfo(oi.getEvenInfo());
					dto.setEvenCriterion(oi.getEvenCriterion());
					dto.setEvenSwitch(oi.getEvenSwitch());
					dto.setDepartmentId(dto.getDepartmentId());
					dto.setDepartmentName(dto.getDepartmentName());
					OmInspectionAreaClassification classification = omInspectionAreaClassificationMapper
							.getName(String.valueOf(dto.getRegionCode()));
					if (ObjectUtil.isNotEmpty(classification)) {
						if (StringUtils.isEmpty(classification.getName()))
							dto.setRegionName("");
						else
							dto.setRegionName(classification.getName());
					}
				}
			}

		}
		return list;
	}

	@Override
	public List<EventAndRegionRespDto> getDeviceList(String regionName, String regionFacilityName) {
		List<EventAndRegionRespDto> list = omInspectionItemsMapper.getDeviceList(regionName, regionFacilityName);
		for (EventAndRegionRespDto dto : list) {
			if (StringUtils.isNotEmpty(dto.getItemsId()) && StringUtils.isNotEmpty(dto.getId())) {
				if (dto.getItemsId() != dto.getId()) {
					dto.setId(dto.getItemsId());
					OmInspectionItems oi = omInspectionItemsMapper.selectById(dto.getItemsId());
					String name = omEvenAndDeviceMapper.getDeviceName(dto.getRegionCode());
					if (ObjectUtil.isEmpty(name))
						dto.setRegionName("");
					else
						dto.setRegionName(name);
					dto.setEvenInfo(oi.getEvenInfo());
					dto.setEvenCriterion(oi.getEvenCriterion());
					dto.setEvenSwitch(oi.getEvenSwitch());
				}
			}

		}
		return list;
	}

	@Override
	public List<OmInspectionItems> getItems(String id) {
		List<OmInspectionItems> list = omInspectionItemsMapper.getItems(id);
		return list;
	}

	@Override
	public OmInspectionItems getItemsById(String id) {
		return omInspectionItemsMapper.getItemsById(id);
	}

	@Override
	public List<OmInspectionItems> getItemsByInfo(String evenInfo, String evenCriterion) {
		return omInspectionItemsMapper.getItemsByInfo(evenInfo, evenCriterion);
	}

	@Override
	public List<OmInspectionItems> getQuipmentItems(String id) {
		List<OmInspectionItems> list = omInspectionItemsMapper.getQuipmentItems(id);
		return list;
	}

	@Override
	public List<EventAndRegionRespDto> getItemsList(String pidList) {
		List<EventAndRegionRespDto> list = omInspectionItemsMapper.getItemsList(pidList);
		return list;
	}

	@Override
	public List<OmInspectionItems> getQuipmentItemsList(String pidList) {
		List<OmInspectionItems> list = omInspectionItemsMapper.getQuipmentItemsList(pidList);
		return list;
	}

	@Override
	public OmInspectionItems getEntity(String info, String criterionInfo) {
		OmInspectionItems om = omInspectionItemsMapper.getEntity(info, criterionInfo);
		return om;
	}

	@Override
	public Page setPage(List<EventAndRegionRespDto> eventAndRegionRespDto, Page page) {
		if (eventAndRegionRespDto == null) {
			return null;
		}
		int size = eventAndRegionRespDto.size();
		int pageSize = (int) page.getSize();
		if (pageSize > size) {
			pageSize = size;
		}
		int pageCurrent = (int) page.getCurrent();
		if (pageSize != 0) {
			// 求出最大页数，防止currentPage越界
			int maxPage = size % pageSize == 0 ? size / pageSize : size / pageSize + 1;

			if (pageCurrent > maxPage) {
				pageCurrent = maxPage;
			}
		}
		// 当前页第一条数据的下标
		int curIdx = pageCurrent > 1 ? (pageCurrent - 1) * pageSize : 0;

		List pageList = new ArrayList();

		// 将当前页的数据放进pageList
		for (int i = 0; i < pageSize && curIdx + i < size; i++) {
			pageList.add(eventAndRegionRespDto.get(curIdx + i));
		}

		page.setCurrent(pageCurrent).setSize(pageSize).setTotal(eventAndRegionRespDto.size()).setRecords(pageList);
		return page;
	}

	@Override
	public Boolean delete(List<DeleteDto> deleteDtoList) {
		for (DeleteDto deleteDto : deleteDtoList) {
			omEvenAndFacilityMapper.deleteByCode(deleteDto.getChild(), deleteDto.getId());
			if (omEvenAndFacilityMapper.getList(deleteDto.getId()).size() <= 0
					&& omDeviceModelClassificationMapper.getModelList(deleteDto.getId()).size() <= 0) {
				omInspectionItemsMapper.deleteById(deleteDto.getId());
			}
			if (omEvenAndFacilityMapper.getchlList(deleteDto.getPId()).size() <= 0) {
				omEvenAndRegionMapper.deleteById(deleteDto.getPId());
			}
		}

		return true;
	}

	@Override
	public Boolean deleteDevice(List<DeleteDto> deleteDtoList) {
		for (DeleteDto deleteDto : deleteDtoList) {
			omDeviceModelClassificationMapper.deleteByCode(deleteDto.getChild(), deleteDto.getId());
			if (omEvenAndFacilityMapper.getList(deleteDto.getId()).size() <= 0
					&& omDeviceModelClassificationMapper.getModelList(deleteDto.getId()).size() <= 0) {
				omInspectionItemsMapper.deleteById(deleteDto.getId());
			}
			if (omDeviceModelClassificationMapper.getchlList(deleteDto.getPId()).size() <= 0) {
				omEvenAndDeviceMapper.deleteById(deleteDto.getPId());
			}
		}
		return true;
	}

	@Override
	public void add(EventAndRegionDto eventAndRegionDto) {
		List<EvenDto> evenDtoList = eventAndRegionDto.getEvenDtoList();
		List<RegionDto> regionDtoList = eventAndRegionDto.getRegionDtoList();
		for (EvenDto dto : evenDtoList) {
			if (regionDtoList.size() > 0 && eventAndRegionDto.getFlag() == 1) {
				for (RegionDto regionDto : regionDtoList) {
					OmInspectionItems om = new OmInspectionItems();
					om.setEvenInfo(dto.getEvenInfo());
					om.setEvenCriterion(dto.getEvenCriterion());
					om.setEvenSwitch(dto.getEvenSwitch());
					om.setDepartmentId(dto.getDepartmentId());
					om.setDepartmentName(dto.getDepartmentName());
					omInspectionItemsMapper.insert(om);
					OmEvenAndFacility of = new OmEvenAndFacility();
					of.setItemsId(om.getId().toString());
					of.setRegionFacilityCode(regionDto.getRegionCode());
					of.setRegionFacilityName(regionDto.getRegionName());
					of.setParentId(eventAndRegionDto.getRegionCode().toString());
					omEvenAndFacilityMapper.insert(of);
				}
			}
			else {
				OmEvenAndRegion oe = new OmEvenAndRegion();
				OmInspectionItems om = new OmInspectionItems();
				om.setEvenInfo(dto.getEvenInfo());
				om.setEvenCriterion(dto.getEvenCriterion());
				om.setEvenSwitch(dto.getEvenSwitch());
				om.setDepartmentId(dto.getDepartmentId());
				om.setDepartmentName(dto.getDepartmentName());
				omInspectionItemsMapper.insert(om);
				oe.setItemsId(om.getId().toString());
				oe.setRegionCode(eventAndRegionDto.getRegionCode().toString());
				oe.setRegionName(eventAndRegionDto.getRegionName());
				omEvenAndRegionMapper.insert(oe);
			}
			if (eventAndRegionDto.getFlag() == 0) {
				// 父类的事项子类必须有继承
				List<RegionDto> list = eventAndRegionDto.getRegionDtoList();
				for (RegionDto regionDto : list) {
					OmInspectionItems om = new OmInspectionItems();
					om.setEvenInfo(dto.getEvenInfo());
					om.setEvenCriterion(dto.getEvenCriterion());
					om.setEvenSwitch(dto.getEvenSwitch());
					om.setDepartmentId(dto.getDepartmentId());
					om.setDepartmentName(dto.getDepartmentName());
					omInspectionItemsMapper.insert(om);
					if (omEvenAndFacilityMapper.getchlidList(regionDto.getRegionCode()).size() <= 0) {
						OmEvenAndFacility facility = new OmEvenAndFacility();
						facility.setItemsId(om.getId().toString());
						facility.setRegionFacilityName(regionDto.getRegionName());
						facility.setRegionFacilityCode(regionDto.getRegionCode());
						facility.setParentId(eventAndRegionDto.getRegionCode().toString());
						omEvenAndFacilityMapper.insert(facility);
					}
				}
			}

		}
	}

	@Override
	public void addDevice(DeviceAndModelDto deviceAndModelDto) {
		List<EvenDto> evenDtoList = deviceAndModelDto.getEvenDtoList();
		List<DeviceDto> deviceDtoList = deviceAndModelDto.getDeviceDtoList();
		for (EvenDto dto : evenDtoList) {
			if (deviceDtoList.size() > 0 && "1".equals(deviceAndModelDto.getFlag())) {
				for (DeviceDto deviceDto : deviceDtoList) {
					OmDeviceModelClassification of = new OmDeviceModelClassification();
					OmInspectionItems om = new OmInspectionItems();
					om.setEvenInfo(dto.getEvenInfo());
					om.setEvenCriterion(dto.getEvenCriterion());
					om.setEvenSwitch(dto.getEvenSwitch());
					om.setDepartmentId(dto.getDepartmentId());
					om.setDepartmentName(dto.getDepartmentName());
					omInspectionItemsMapper.insert(om);
					of.setItemsId(om.getId().toString());
					of.setDeviceModelClassification(deviceDto.getRegionCode());
					of.setDeviceModelClassificationName(deviceDto.getRegionName());
					of.setParentId(deviceAndModelDto.getRegionCode());
					omDeviceModelClassificationMapper.insert(of);
				}
			}
			else {
				OmInspectionItems om = new OmInspectionItems();
				om.setEvenInfo(dto.getEvenInfo());
				om.setEvenCriterion(dto.getEvenCriterion());
				om.setEvenSwitch(dto.getEvenSwitch());
				om.setDepartmentId(dto.getDepartmentId());
				om.setDepartmentName(dto.getDepartmentName());
				omInspectionItemsMapper.insert(om);
				OmEvenAndDevice oe = new OmEvenAndDevice();
				oe.setItemsId(om.getId().toString());
				oe.setDeviceCode(deviceAndModelDto.getRegionCode());
				oe.setDeviceName(deviceAndModelDto.getRegionName());
				omEvenAndDeviceMapper.insert(oe);
			}
			if ("0".equals(deviceAndModelDto.getFlag())) {
				// 父类的事项子类必须有继承
				List<DeviceDto> list = deviceAndModelDto.getDeviceDtoList();
				for (DeviceDto deviceDto : list) {
					OmInspectionItems om = new OmInspectionItems();
					om.setEvenInfo(dto.getEvenInfo());
					om.setEvenCriterion(dto.getEvenCriterion());
					om.setEvenSwitch(dto.getEvenSwitch());
					om.setDepartmentId(dto.getDepartmentId());
					om.setDepartmentName(dto.getDepartmentName());
					omInspectionItemsMapper.insert(om);
					if (omDeviceModelClassificationMapper.getchlidList(deviceDto.getRegionCode()).size() <= 0) {
						OmDeviceModelClassification omDeviceModelClassification = new OmDeviceModelClassification();
						omDeviceModelClassification.setItemsId(om.getId().toString());
						omDeviceModelClassification.setDeviceModelClassificationName(deviceDto.getRegionName());
						omDeviceModelClassification.setDeviceModelClassification(deviceDto.getRegionCode());
						omDeviceModelClassification.setParentId(deviceAndModelDto.getRegionCode());
						omDeviceModelClassificationMapper.insert(omDeviceModelClassification);
					}
				}
			}
		}
	}

}
